﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartSpace.Core;
using SsTool.Base.Windows;

namespace SsTool
{
    /// <summary>
    /// Interaction logic for UserListPage.xaml
    /// </summary>
    public partial class UserListPage : Page
    {
        public UserListPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(UserListPage_Loaded);
        }

        void UserListPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadDatas();
        }

        private void LoadDatas()
        {
            ICoreService service = App.Get<ICoreService>();
            _ctlGridUser.ItemsSource = service.GetUsers(0, 999);
        }

        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            CreateUserWindow window = new CreateUserWindow();
            if (window.ShowDialog() == true)
            {
                LoadDatas();
            }
        }

        private void _ctlBtnUpdate_Click(object sender, RoutedEventArgs e)
        {
            User selectedUser = _ctlGridUser.SelectedItem as User;
            if (selectedUser != null)
            {
                UpdateUserWindow window = new UpdateUserWindow(selectedUser);
                if (window.ShowDialog() == true)
                {
                    LoadDatas();
                }
            }
            else
            {
                MessageBox.Show("请选择一个用户!");
            }
        }

        private void _ctlBtnDelete_Click(object sender, RoutedEventArgs e)
        {
            User selectedUser = _ctlGridUser.SelectedItem as User;
            if (selectedUser != null)
            {
                if (MessageBox.Show("你是否要删除用户[" + selectedUser.Name + "]?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    ICoreService coreService = App.Get<ICoreService>();
                    if (coreService.RemoveUser(selectedUser))
                    {
                        LoadDatas();
                    }
                    else
                    {
                        MessageBox.Show("删除失败!");
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择一个用户!");
            }
        }
    }
}
